package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.ActorDao;
import cn.edu.sdjzu.xg.bysj.dao.StudentDao;
import cn.edu.sdjzu.xg.bysj.dao.UserDao;

import cn.edu.sdjzu.xg.bysj.domain.Student;
import cn.edu.sdjzu.xg.bysj.domain.Teacher;
import cn.edu.sdjzu.xg.bysj.domain.User;

import test.BysjException;
import util.JdbcHelper;
import util.Pagination;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class StudentService {
    private static StudentDao studentDao =  StudentDao.getInstance();
    private static StudentService studentService= new StudentService();

    private StudentService() {
    }
    public static StudentService getInstance() {
        return studentService;
    }

    public int countAll(Teacher teacher) throws SQLException {
        return studentDao.countAll(teacher);
    }
    public Collection<Student> findAll(Teacher teacher, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<Student> students =studentDao.findAll(teacher,pagination,connection) ;
        JdbcHelper.close(connection);
        return students;
    }
    public Collection<Student> findAll(List conditionList, Pagination pagination) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<Student> students =studentDao.findAll(conditionList,pagination,connection) ;
        JdbcHelper.close(connection);
        return students;
    }

    public Student find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Student student = studentDao.find(id,connection);
        JdbcHelper.close(connection);
        return student;
    }

    public boolean update(Student student) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Boolean updated = studentDao.update(student,connection);
        JdbcHelper.close(connection);
        return updated ;
    }

    public Boolean add(Student student) throws SQLException ,BysjException{
        //获得进行事务的连接，添加教师和添加用户都使用本连接对象
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false，开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            //添加actor对象，返回值为自动生成的id
            int actorId = ActorDao.getInstance().add(connection);
            student.setId(actorId);
            studentDao.add(student, connection);
            //创建用户对象，密码和用户名取自teacher字段值no
            User user = new User(student.getNo(), student.getNo(), null, student);
            //增加用户
            added = UserDao.getInstance().add(user, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
            throw new BysjException("失败");
        } finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        boolean deleted = studentDao.delete(id, connection);
        JdbcHelper.close(connection);
        return deleted;
    }

    public boolean delete(Student student) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        boolean deleted = studentDao.delete(student, connection);
        JdbcHelper.close(connection);
        return deleted;
    }
}
